﻿//////////////////////////////////////////////////////////////////
//
// AuditLogManager.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Reflection;

using log4net.Core;

namespace log4net.Ext.Audit
{
   public class AuditLogManager
   {
      public static Type ThisDeclaringType = typeof(AuditLogImpl);
      private static readonly WrapperMap s_wrapperMap =
        new WrapperMap(new WrapperCreationHandler(WrapperCreationHandler));

      private AuditLogManager() { }

      public static IAuditLog Exists(string name)
      {
         return Exists(Assembly.GetCallingAssembly(), name);
      }

      public static IAuditLog Exists(string domain, string name)
      {
         return WrapLogger(LoggerManager.Exists(domain, name));
      }

      public static IAuditLog Exists(Assembly assembly, string name)
      {
         return WrapLogger(LoggerManager.Exists(assembly, name));
      }

      public static IAuditLog[] GetCurrentLoggers()
      {
         return GetCurrentLoggers(Assembly.GetCallingAssembly());
      }

      public static IAuditLog[] GetCurrentLoggers(string domain)
      {
         return WrapLoggers(LoggerManager.GetCurrentLoggers(domain));
      }

      public static IAuditLog[] GetCurrentLoggers(Assembly assembly)
      {
         return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly));
      }

      public static IAuditLog GetLogger(string name)
      {
         return GetLogger(Assembly.GetCallingAssembly(), name);
      }

      public static IAuditLog GetLogger(string domain, string name)
      {
         return WrapLogger(LoggerManager.GetLogger(domain, name));
      }

      public static IAuditLog GetLogger(Assembly assembly, string name)
      {
         return WrapLogger(LoggerManager.GetLogger(assembly, name));
      }

      public static IAuditLog GetLogger(Type type)
      {
         return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
      }

      public static IAuditLog GetLogger(string domain, Type type)
      {
         return WrapLogger(LoggerManager.GetLogger(domain, type));
      }

      public static IAuditLog GetLogger(Assembly assembly, Type type)
      {
         return WrapLogger(LoggerManager.GetLogger(assembly, type));
      }

      private static IAuditLog WrapLogger(ILogger logger)
      {
         return (IAuditLog)s_wrapperMap.GetWrapper(logger);
      }

      private static IAuditLog[] WrapLoggers(ILogger[] loggers)
      {
         IAuditLog[] results = new IAuditLog[loggers.Length];
         for (int i = 0; i < loggers.Length; i++)
         {
            results[i] = WrapLogger(loggers[i]);
         }
         return results;
      }

      private static ILoggerWrapper WrapperCreationHandler(ILogger logger)
      {
         return new AuditLogImpl(logger);
      }
   }
}
